package dao

import (
	"myserver/model"
	"myserver/param"
	"myserver/tool"
)

//
type UserDao struct {
	*tool.MyOrm
}

func (d *UserDao) QueryUser(id string) model.User {
	var user = model.User{}
	d.Db.Where("id = ?", id).First(&user)
	return user
}

func (d *UserDao) CreateUser(user model.User) int64 {
	res := d.Db.Create(&user)
	return res.RowsAffected
}

func (d *UserDao) QueryUserByNamePwd(name string) model.User {
	var user = model.User{}
	d.Db.Where("user_name = ?", name).First(&user)
	return user
}

func (d *UserDao) QueryIsByName(user model.User) bool {
	var user1 model.User
	d.Db.Where("user_name = ?", user.UserName).First(&user1)
	if user1.ID != 0 {
		return true
	}
	return false
}

func (d *UserDao) DeleteUser(user model.User) int64 {
	res := d.Db.Delete(&user)
	return res.RowsAffected
}

func (d *UserDao) UpdateUser(user model.User) int64 {
	res := d.Db.Model(&user).Updates(user)
	return res.RowsAffected
}

func (d *UserDao) QueryUsers(pageParam param.PageParam) param.ResponsePageParam {
	rs := param.ResponsePageParam{}
	rs.Data = []model.User{}
	db := d.Db.Model(&model.User{})
	if pageParam.SearchContent != "" {
		db = db.Where("user_name LIKE ?", "%"+pageParam.SearchContent+"%")
	}
	db.Order("ID ASC").Count(&rs.Total).Limit(pageParam.Limit).Offset(pageParam.Page).Find(&rs.Data)
	return rs
}

func (d *UserDao) QueryUserId(name string) uint {
	var user1 model.User
	d.Db.Where("user_name = ?", name).First(&user1)
	return user1.ID
}
